package org.orzeszek.transfer.comm;

import java.io.File;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * The {@link FileHelper} provides assistance managing hosted files on the
 * user's machine.
 * 
 * @author Michael Gierasimiuk
 */
public class FileHelper
{
  public static final String HASH_ALGORITHM = "SHA-512";

  private MessageDigest digest;

  /**
   * Creates a new {@link FileHelper} instance.
   */
  public FileHelper()
  {
    try
    {
      this.digest = MessageDigest.getInstance( HASH_ALGORITHM );
    }
    catch ( NoSuchAlgorithmException e )
    {
      e.printStackTrace();
    }
  }

  /**
   * Creates a temporary file and returns an implementation of {@link File} for
   * the newly created file.
   * 
   * @return the new temporary file.
   */
  public File createTempDirectory()
  {
    try
    {
      return File.createTempFile( "temp", Long.toString( System.nanoTime() ) );
    }
    catch ( IOException e )
    {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * Creates a has of the given filename's complete path using the SHA-512
   * algorithm.
   * 
   * @param aFileToHash the file to has the path of.
   * @return the has in {@link String} format.
   */
  public String createHash( File aFileToHash )
  {
    this.digest.update( aFileToHash.getAbsolutePath().getBytes() );

    return String.valueOf( this.digest.digest() );
  }

  /** TODO: DEBUG ONLY. */
  public static void main( String... args )
  {

  }
}
